{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `pandas_bridge`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `evo.tools.pandas_bridge` module converts:\n",
    "* `evo.core.trajectory.PosePath3D`\n",
    "* `evo.core.trajectory.PoseTrajectory3D`\n",
    "* `evo.core.result.Result`\n",
    "\n",
    "to Pandas dataframes.\n",
    "\n",
    "Load some required modules for this demo:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# magic plot configuration\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "%matplotlib inline\n",
    "%matplotlib notebook\n",
    "\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load Pandas and `pandas_bridge`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from evo.tools import pandas_bridge\n",
    "from evo.tools import file_interface"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trajectories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load a trajectory from a ROS bag...\n",
    "from rosbags.rosbag1 import Reader as Rosbag1Reader\n",
    "with Rosbag1Reader(\"../test/data/ROS_example.bag\") as reader:\n",
    "    traj = file_interface.read_bag_trajectory(reader, \"S-PTAM\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ...or from a KITTI file...\n",
    "traj = file_interface.read_kitti_poses_file(\"../test/data/KITTI_00_gt.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ...or from a TUM file...\n",
    "traj = file_interface.read_tum_trajectory_file(\"../test/data/fr2_desk_ORB.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Convert a trajectory to a dataframe:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "traj_df = pandas_bridge.trajectory_to_df(traj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Some examples for what you can do with it:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"First entries of the dataframe:\")\n",
    "display(traj_df.head())\n",
    "\n",
    "print(\"Some statistics of the dataframe:\")\n",
    "display(traj_df.describe())\n",
    "\n",
    "print(\"A plot:\")\n",
    "traj_df[[\"x\", \"y\", \"z\"]].plot(kind=\"line\", subplots=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate some result files\n",
    "import subprocess as sp\n",
    "cmd_1 = \"evo_ape kitti ../test/data/KITTI_00_gt.txt ../test/data/KITTI_00_ORB.txt --save_results ../test/data/res1.zip --no_warnings\"\n",
    "cmd_2 = \"evo_ape kitti ../test/data/KITTI_00_gt.txt ../test/data/KITTI_00_SPTAM.txt --save_results ../test/data/res2.zip --no_warnings\"\n",
    "sp.call(cmd_1.split(\" \"))\n",
    "sp.call(cmd_2.split(\" \"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_1 = file_interface.load_res_file(\"../test/data/res1.zip\")\n",
    "result_2 = file_interface.load_res_file(\"../test/data/res2.zip\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Convert results into individual dataframes and concatenate them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_df_1 = pandas_bridge.result_to_df(result_1)\n",
    "result_df_2 = pandas_bridge.result_to_df(result_2)\n",
    "\n",
    "result_df = pd.concat([result_df_1, result_df_2], axis=\"columns\")\n",
    "display(result_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Some examples for what you can do with it:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display(result_df.loc[\"stats\"])\n",
    "\n",
    "exclude = result_df.loc[\"stats\"].index.isin([\"sse\"])  # don't plot sse\n",
    "result_df.loc[\"stats\"][~exclude].plot(kind=\"bar\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
